package com.hnair.bot.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hnair.bot.base.BaseController;
import com.hnair.bot.common.result.BaseResult;
import com.hnair.bot.model.BfDicttype;
import com.hnair.bot.model.BfUser;
import com.hnair.bot.model.PageInfo;
import com.hnair.bot.service.IBfDicttypeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/****
 * 字典类型表
 * 
 * @author zhulei
 *
 */
@Controller
public class DictTypeController extends BaseController{
  
	private final Logger logger = LoggerFactory.getLogger(DictTypeController.class);
	
	@Autowired
	private IBfDicttypeService dictTypeService;
	
	@RequestMapping("/dictType/index")
	public String index(HttpServletRequest request) {
		return "pages/dictType/index";
	}
	
	@RequestMapping("/dictType/add")
	public ModelAndView add(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("pages/dictType/add");
		return mav;
	}
	
	
	@RequestMapping("/dictType/edit")
	public ModelAndView edit(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("pages/dictType/edit");
		modelAndView.addObject("id", request.getParameter("id"));
		return modelAndView;
	}
	
	
	
	/**
	 * 字典类型列表
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/dictTypeList" }, method = RequestMethod.GET)
	public @ResponseBody
	BaseResult dictTypeList(HttpServletRequest request,BfDicttype dictType,PageInfo pageInfo) {
		

		try {
			
			Wrapper<BfDicttype> wrapper=new EntityWrapper<BfDicttype>();
    		
        	if(StringUtils.isNotEmpty( dictType.getTypeName())){
        		wrapper.like("type_name", dictType.getTypeName());
        	}
        	
        	if(StringUtils.isNotEmpty( dictType.getTypeDesc())){
        		wrapper.like("type_desc",dictType.getTypeDesc());
        	}
       	
    		Page<BfDicttype> page=new Page<BfDicttype>(pageInfo.getPage(),pageInfo.getRows());
    		  Page<BfDicttype> dictList = dictTypeService.selectPage(page, wrapper);
//    		userList.getRecords();
//    		userList.getTotal();
    		Map res = new HashMap();
    		res.put("rows", dictList.getRecords());
			res.put("total", dictList.getTotal());				
			
			
			return BaseResult.success(res);
		} catch (Exception e) {
			logger.error("操作异常" , e);
			return BaseResult.exception(e.getMessage());
		}

	}
	
	
	/**
	 * 添加字典类型
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/dictTypeAdd" }, method = RequestMethod.POST)
	public @ResponseBody
	BaseResult dictTypeAdd(HttpServletRequest request,BfDicttype dictType) {

		try {
			BfUser user=(BfUser)request.getSession().getAttribute("userInfo");			
			dictTypeService.insert(dictType);
			
			this.saveUserlog(request, "字典类型管理："+user.getUsername()+"添加操作");
			return BaseResult.success();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("操作异常" , e);
			return BaseResult.exception(e.getMessage());
		}

	}
	
	
	/**
	 * 获取字典类型详情
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/dictTypeInfo" }, method = RequestMethod.GET)
	public @ResponseBody
	BaseResult dictTypeInfo(HttpServletRequest request,Long id) {

		BfDicttype dictType = null;
		try {
			dictType = dictTypeService.selectById(id);
			// res.put("success", "true");
			return BaseResult.success(dictType);
		} catch (Exception e) {	
			logger.error("操作异常" , e);
			return BaseResult.exception(e.getMessage());
		}

	}
	
	
	/**
	 * 字典类型更新
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/dictTypeUpdate" }, method = RequestMethod.POST)
	public @ResponseBody
	BaseResult dictTypeUpdate(HttpServletRequest request,BfDicttype dictType) {

		Map res = new HashMap();
		try {
			BfUser user=(BfUser)request.getSession().getAttribute("userInfo");			
			dictTypeService.updateAllColumnById(dictType);
			this.saveUserlog(request, "字典类型管理："+user.getUsername()+"修改操作");
			return BaseResult.success();
		} catch (Exception e) {
			logger.error("操作异常" , e);
			return BaseResult.exception(e.getMessage());
		}

	}

	
	/**
	 * restful
	 * 删除字典类型
	 * @param req
	 * @return
	 */
	@RequestMapping(value = { "/rest/dictTypeDel" }, method = RequestMethod.POST)
	public @ResponseBody
	BaseResult dictTypeDel(HttpServletRequest request,String ids) {
		try {
			BfUser user=(BfUser)request.getSession().getAttribute("userInfo");	
//			String[] ids = request.getParameter("ids").split(",");
//			dictTypeService.deleteByPrimaryKeys(ids);

			dictTypeService.deleteBatchIds(Arrays.asList(ids.split(",")));
			
			this.saveUserlog(request, "字典类型管理："+user.getUsername()+"删除操作");
			return BaseResult.success();
		} catch (Exception e) {
			logger.error("操作异常" , e);
			return BaseResult.exception(e.getMessage());
		}
	}
	
//	@RequestMapping(value = { "/rest/api/searchDictType" }, method = RequestMethod.GET)
//	public @ResponseBody
//	BaseResult searchDictType(HttpServletRequest request,String typeName) throws IllegalAccessException, InvocationTargetException {
//		try {
//			DictTypeExample example = new DictTypeExample();
//			example.createCriteria().andTypeNameEqualTo(typeName);
//			List<DictType> list = dictTypeService.selectByExample(example);
//			if(list == null || list.size()==0)
//			{
//				return BaseResult.fail("0002", "未查询到值");
//			}
//			return BaseResult.success(list.get(0));
//		} catch (Exception e) {
//			logger.error("操作异常" , e);
//			return BaseResult.exception(e.getMessage());
//		}
//	}
	
}
